/**************************************************************************************************
 **Company       : BURNON TECHNOLOGY LTD.
 **Project Name	 : dsPIC33CK64MP206 BOOT
 **File:         : Framework.c
 **Created by  	 : YangWQ
 **Created Date	 : June 18, 2019, 1:19 PM
 **Version:      : v0.1
 **Target  		 : dsPIC33CK64MP206
 **HardWare		 : 
 **SofeWare      : MPLAB X IDE v5.20 + XC16 v1.36 
 **************************************************************************************************/

// This is a guard condition so that contents of this file are not included
// more than once.  
#ifndef XC_FRAMEWORK_H
#define	XC_FRAMEWORK_H

#include "../GenericTypeDefs.h"
#include "sys/kmem.h"
#include "../xc.h"

//---- HEX? ?? -------------
#define DATA_RECORD 		  0
#define END_OF_FILE_RECORD 	  1
#define EXT_SEG_ADRS_RECORD   2
#define EXT_LIN_ADRS_RECORD   4



typedef struct 
{
	uint8_t RecDataLen;
	DWORD_VAL Address;
	uint8_t RecType;
	uint8_t* Data;
	uint8_t CheckSum;
	DWORD_VAL ExtSegAddress;
	DWORD_VAL ExtLinAddress;
}T_HEX_RECORD;

#define FRAMEWORK_ExitFirmwareUpgradeMode 	ExitFirmwareUpgradeMode
#define FRAMEWORK_BuildRxFrame  			BuildRxFrame
#define FRAMEWORK_GetTransmitFrame 			GetTransmitFrame
#define FRAMEWORK_FrameWorkTask 			FrameWorkTask

extern BOOL ExitFirmwareUpgradeMode(void);
extern void BuildRxFrame(uint8_t *RxData, INT16 RxLen);
extern UINT GetTransmitFrame(uint8_t* Buff);
extern void FrameWorkTask(void);
extern UINT WriteHexRecord2Flash(uint8_t* HexRecord, UINT totalRecLen);


#define  CMD_TO_SLAVE_1_FLAY   0x00   
#define  CMD_TO_SLAVE_2_FLAY   0x40   

#define  CMD_SPI_READ_FLAY     0x80   

#define SOH 01
#define EOT 04
#define DLE 16

#define FRAMEWORK_BUFF_SIZE					100 //1000


typedef struct
{
	UINT  Len; 
    union
    {
        uint8_t Buf[FRAMEWORK_BUFF_SIZE];
        struct
        {
            uint8_t Cmd;                         
            uint8_t Data[FRAMEWORK_BUFF_SIZE-1]; 
        }Frame;
    };	
}T_UART_FRAME;


typedef enum
{
    R_OK = 0,      
    R_INVALID,       
    R_NonExecution,   
    R_ExecutionERROR  
}T_RUNING_STATE;

//The default program version is 2025-04-15-1
#define  DFS_Version_Year     25          
#define  DFS_Version_Month    04
#define  DFS_Version_Day      15
#define  DFS_Version_Number    1


typedef enum {
    NORMAL_MODE = 0x00,
    PROGRAM_MODE = 0x01
} BootMode;

#define USER_APP_START_ADDRESS 		 ((uint32_t)(PA_TO_KVA0(0x1d000200U + 0x10000)))

#define USER_ERASE_ADDRESS 		 ((uint32_t)(PA_TO_KVA0(0x1d000000U + 0x10000)))

#define USER_APP_END_ADDRESS 		 ((uint32_t)(PA_TO_KVA0(0x1d0FF000U)))
#define Flash_Page_Size              (4096U)

#define  USER_MSG_ADDRESS 		  ((uint32_t)(PA_TO_KVA0(0x1d0FF000U)))
#define  APP_Flay_FlashAddr       ((uint32_t)(PA_TO_KVA0(0x1d0FF000U)))
#define  DEF_APP_FLAY   0xAA

extern  BOOL GetChar(uint8_t *byte);
extern  void PutChar(uint8_t txChar);

BootMode GetMode(void);
uint8_t GetSlaveNum(void);
void SetModeSlave(BootMode mode, uint8_t slaveNum);
BOOL IsEraseFlashEnabled(void);
BOOL IsProgramFlashEnabled(void);
void SetProgramFlashEnabled(BOOL enabled);
void SetEraseFlashEnabled(BOOL enabled);
uint16_t GetCrcData(void);

void UART_Response_Func(void);
void RotateAxis_Response_Func(void);
uint16_t Crc_Fun(uint8_t *data, uint32_t len, uint16_t init_crc);
uint16_t CRC16_Chk(uint8_t *puchMsg, uint8_t usDataLen);

#endif	/* XC_HEADER_TEMPLATE_H */

